package org.geektimes.configuration.microprofile.config;

import org.eclipse.microprofile.config.Config;
import org.eclipse.microprofile.config.spi.ConfigBuilder;
import org.eclipse.microprofile.config.spi.ConfigSource;
import org.eclipse.microprofile.config.spi.Converter;
import org.geektimes.configuration.microprofile.config.converters.Converters;
import org.geektimes.configuration.microprofile.config.sources.ConfigSources;

public class DefaultConfigBuilder implements ConfigBuilder{
	
	private ConfigSources configSources;
	
	private Converters converters;
	
	public DefaultConfigBuilder(ClassLoader classLoader) {
		this.configSources = new ConfigSources(classLoader);
		this.converters = new Converters(classLoader);
	}
  
	@Override
	public ConfigBuilder addDefaultSources() {
		configSources.addDefaultSources();;
		return this;
	}

	@Override
	public ConfigBuilder addDiscoveredSources() {
		configSources.addDiscoveredSources();
		return this;
	}

	@Override
	public ConfigBuilder addDiscoveredConverters() {
		converters.addDiscovertedConverters();
		return this;
	}

	@Override
	public ConfigBuilder forClassLoader(ClassLoader loader) {
		this.configSources.setClassLoader(loader);
		this.converters.setClassLoader(loader);
		return this;
	}

	@Override
	public ConfigBuilder withSources(ConfigSource... sources) {
		configSources.addSources(sources);
		return this;
	}

	@Override
	public ConfigBuilder withConverters(Converter<?>... converters) {
		this.converters.addConverters(converters);
		return this;
	}

	@Override
	public <T> ConfigBuilder withConverter(Class<T> type, int priority, Converter<T> converter) {
		this.converters.addConverter(converter, type, priority);
		return this;
	}

	@Override
	public Config build() {
		return new DefaultConfig(configSources, converters);
	}

}
